/* Modle */
var names = new Array(); 
var links = new Array(); 
var flags = new Array(); 

if("arguments" in window && window.arguments.length > 0) 
{

	for ( var k = 0; k < 2; k++ ) 
	{
		for (var i = 0; i < window.arguments[k].length; i++) 
		{
			var link = window.arguments[k][i].toLowerCase();
			if (link.lastIndexOf('?')>=0) link = link.substring(0, link.lastIndexOf('?')-1);
			
			var link_name = link;
			link_name = link_name.substring(link_name.lastIndexOf('/') + 1);
			
			if (link_name.length <= 0 ) continue; 
			if (link_name.lastIndexOf('.')<=0) continue;
			if (link_name.length - link_name.lastIndexOf('.') - 1 > 5) continue;
			if (havelink(links, link)) continue;
			
			names.push( i+'_'+link_name );	
			links.push( link );
			flags.push( false );//true
		}
	}
}


function havelink(links, link){

for(var i=0; i<links.length; i++){
	if (links[i] == link) return true;	
}
return false;


}

/* Control */
function loadList(){

	// Get links
	var list = document.getElementById('list-link');
	
	// Clear links
	var count = list.itemCount;
	while (count-- > 0){ list.removeItemAt(0); }
		

	// Load links
	for (var i = 0; i < links.length; i++)
		{
			var row  = document.createElement('listitem');
			var cell = document.createElement('listcell');
			if (flags[i]) cell.setAttribute('label', '*');
			else          cell.setAttribute('label', '');
			row.appendChild(cell);
			
			cell = document.createElement('listcell');
			cell.setAttribute('label', i+1);
			row.appendChild(cell);
			
			cell = document.createElement('listcell');
			cell.setAttribute('label',  names[i] ); 
			row.appendChild(cell);
			
			cell = document.createElement('listcell');
			cell.setAttribute('label',  links[i] ); 
			row.appendChild(cell);
				
			list.appendChild(row);
		}
}

function SaveFile(path, name, url) {
    // file name	
    var fileName = path + name;
	
    // url
    var obj_URL = Components.classes["@mozilla.org/network/io-service;1"]
              .getService(Components.interfaces.nsIIOService)
              .newURI( url, null, null);
	
    // file
    var file = Components.classes["@mozilla.org/file/local;1"]
           .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(fileName);

    // persist
    var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
              .createInstance(Components.interfaces.nsIWebBrowserPersist);
    persist.saveURI(obj_URL, null, null, null, "", file);

}

var filePath = "";

function Browse() {
	
	var nsIFilePicker = Components.interfaces.nsIFilePicker;
	var fp = Components.classes['@mozilla.org/filepicker;1'].createInstance(nsIFilePicker);
	fp.init(window, 'Browse for Folder', nsIFilePicker.modeGetFolder);
	fp.appendFilters(nsIFilePicker.filterAll);
	var rv = fp.show();

	if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
	    filePath = fp.file.path;
		document.getElementById('save-at-text').value = filePath;
	}
}

function allFiles( box ){
	for (var i = 0; i < flags.length; i++)
		{ flags[i] = box.checked; }
	loadList();
	
	document.getElementById('f-img').checked = box.checked;
	document.getElementById('f-ado').checked = box.checked;
	document.getElementById('f-vdo').checked = box.checked;
	document.getElementById('f-arc').checked = box.checked;
	document.getElementById('f-doc').checked = box.checked;
	document.getElementById('f-web').checked = box.checked;
	document.getElementById('f-sft').checked = box.checked;
}   

function getext( name ) {
	return name.substring( name.lastIndexOf( '.' ) + 1 );
}

function allImage( box ){

	for (var i = 0; i < flags.length; i++) {
		var ext = getext( names[ i ] );
			if ( ext == "jpg" || ext == "jpeg" || ext == "tiff" || ext == "png" || ext == "gif" || ext == "bmp" ) {
			flags[i] = box.checked;
		}
	}
	loadList();
}

function allAudio( box ){
	for (var i = 0; i < flags.length; i++) {
		var ext = getext( names[ i ] );
		if ( ext == "mp3" || ext == "wav" || ext == "wmv" || ext == "ogg" ) {
			flags[i] = box.checked;
		}
	}
	loadList();
}

function allVideo( box ){
	for (var i = 0; i < flags.length; i++) {
		var ext = getext( names[ i ] );
		if ( ext == "mp4" || ext == "avi" || ext == "mov" || ext == "flv" || ext == "swf" || ext == "rm" ) {
			flags[i] = box.checked;
		}
	}
	loadList();
}

function allArch( box ){
	for (var i = 0; i < flags.length; i++) {
		var ext = getext( names[ i ] );
		if ( ext == "zip" || ext == "rar" || ext == "7z" ) {
			flags[i] = box.checked;
		}
	}
	loadList();
}

function allDocs( box ){
	for (var i = 0; i < flags.length; i++) {
		var ext = getext( names[ i ] );
		if ( ext == "pdf" || ext == "doc" || ext == "ppt" || ext == "xle" ) {
			flags[i] = box.checked;
		}
	}
	loadList();
}


function allWebs( box ){
	for (var i = 0; i < flags.length; i++) {
		var ext = getext( names[ i ] );
		if ( ext == "htm" || ext == "html" || ext == "xml" || ext == "php" || ext == "asp" || ext == "js" ) {
			flags[i] = box.checked;
		}
	}
	loadList();
}

function allSoft( box ){
	for (var i = 0; i < flags.length; i++) {
		var ext = getext( names[ i ] );
		if ( ext == "exe" || ext == "xpi" ) {
			flags[i] = box.checked;
		}
	}
	loadList();
}

function SaveFiles() {
	for (var i=0; i<links.length; i++)
		SaveFile( filePath + "\\", names[i], links[i]);
}